using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

namespace TestApiWithValidUserId
{
    class UserDto
    {
        public int Id { get; set; }
        public string UserName { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("测试修复后的AddFile接口(使用可能存在的用户ID)...");
                
                // 创建HttpClient
                using var httpClient = new HttpClient();
                httpClient.BaseAddress = new Uri("http://localhost:5269/");
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                
                // 尝试查询系统中是否存在用户
                Console.WriteLine("尝试查询系统中的第一个用户...");
                var usersResponse = await httpClient.GetAsync("api/user");
                int? validUserId = null;
                
                if (usersResponse.IsSuccessStatusCode)
                {
                    var usersContent = await usersResponse.Content.ReadAsStringAsync();
                    var users = JsonSerializer.Deserialize<List<UserDto>>(usersContent);
                    
                    if (users != null && users.Count > 0)
                    {
                        validUserId = users[0].Id;
                        Console.WriteLine($"找到有效用户，用户ID: {validUserId}，用户名: {users[0].UserName}");
                    }
                }
                
                if (!validUserId.HasValue)
                {
                    Console.WriteLine("系统中没有找到用户，将使用默认用户ID=1进行测试");
                    validUserId = 1;
                }
                
                // 构造请求体 - 使用可能存在的用户ID
                var requestBody = new
                {
                    FileName = "带用户ID的测试文件.txt",
                    SavedPath = "test/path/user_test.txt",
                    FileTypeId = 1L,
                    UserId = validUserId.Value, // 使用int类型的UserId
                    FtpPath = "ftp/test/path/user_test.txt",
                    Time = DateTime.Now,
                    OpenTime = DateTime.Now
                };
                
                // 序列化请求体
                string jsonContent = JsonSerializer.Serialize(requestBody);
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                
                Console.WriteLine($"请求体内容: {jsonContent}");
                Console.WriteLine($"发送请求到: http://localhost:5269/api/file/add");
                
                // 发送请求
                var response = await httpClient.PostAsync("api/file/add", content);
                
                // 获取响应内容
                string responseContent = await response.Content.ReadAsStringAsync();
                
                // 输出结果
                Console.WriteLine($"响应状态码: {response.StatusCode}");
                Console.WriteLine($"响应内容: {responseContent}");
                
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("✅ 测试成功！使用用户ID的AddFile接口调用成功。");
                }
                else
                {
                    Console.WriteLine("❌ 测试失败！使用用户ID的AddFile接口调用失败。");
                    Console.WriteLine("注意：如果用户ID不存在，这是正常的错误信息");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生异常: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }
            
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}